package com.example.demo.design_pattern.observer.impl;

import com.example.demo.design_pattern.observer.Observerable;
import com.example.demo.design_pattern.observer.edObserverable;
import java.util.ArrayList;
import java.util.List;

/**
 * @author xkx
 * @version 1.0.0
 * @datetime 2019/1/22 18:07
 * @description
 */
public class edObserverableImpl implements edObserverable {

  private List<Observerable> list;
  private String message;

  public edObserverableImpl() {
    this.list = new ArrayList<Observerable>();
  }

  @Override
  public void registerObserver(Observerable observerable) {
    list.add(observerable);
  }

  @Override
  public void removeObserver(Observerable observerable) {
    if (!list.isEmpty()) {
      list.remove(observerable);
    }
  }

  @Override
  public void notifyObserver() {
    for (int i = 0; i < list.size(); i++) {
      list.get(i).update(message);
    }
  }

  public void setInfomation(String s) {
    this.message = s;
    System.out.println("微信服务更新消息： " + s);
    //消息更新，通知所有观察者
    notifyObserver();
  }
}
